//index.js
//获取应用实例
const urlApi = require('../../utils/server.api.js')
const app = getApp()
Page({
  data: {
    hidden: true,
    btnValue: "",
    btnDisabled:false,
    username: "",
    code: "",
    second: 60
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  setUserInfo: function (res) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  create_login: function (e) {
    if (e.detail.value["username"] == "" || e.detail.value["password"] == "" || e.detail.value["password2"] == "" || this.data.code == "") {
      wx.showToast({
        title: "必填信息不能为空",
        icon: 'none',
        duration: 2000
      })
    } else if (e.detail.value["username"] != "" && e.detail.value["password"] != "" && e.detail.value["password2"] != "" && this.data.code != "") {
      if (!(e.detail.value["password"] == e.detail.value["password2"]) || !/^1[3456789]\d{9}$/.test(e.detail.value["username"])) {
        wx.showToast({
          title: "手机号格式错误或两次输入密码不一致",
          icon: 'none',
          duration: 2000
        })
      } else {
        wx.request({
          url: urlApi.patientUpdatePassword() + "?username=" + e.detail.value["username"] + "&password=" + e.detail.value["password"] + "&code=" + this.data.code,
          method: "put",
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res.data);
            if(res.data.code == 200){
              app.globalData.userInfo = res.data.data,
              wx.showToast({
                title: "修改成功",
                duration: 2000
              })
              wx.switchTab({
                url: '../index/index',
              })
              setTimeout(function () {
                wx.navigateBack({
                  delta: 2
                })
              }, 1000)
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 3000
              })
              setTimeout(function () {
                wx.navigateBack({
                  delta: 2
                })
              }, 1000)
            }
          }
        })
      }
    }
  },

  goto_index:function(res){
    
  },

  //手机号输入
  bindPhoneInput(e) {
    var val = e.detail.value;
    this.setData({
      username: val
    })
    if(val != ''){
      this.setData({
        hidden: false,
        btnValue: '获取验证码'
      })
    }else{
      this.setData({
        hidden: true
      })
    }
  },
  //验证码输入
  bindCodeInput(e) {
    this.setData({
      code: e.detail.value
    })
  },

  //获取短信验证码
  getCode(e) {
    console.log('获取验证码');
    var that = this;
    wx.request({
      url: urlApi.sendSms() + "/" + that.data.username,
      method: "post",
      header: {
        'content-type': 'application/json'
      },
      success: function (response) {
        if (response.data.code == 200) {
          that.timer();
          wx.showModal({
            title: '提示',
            content: '验证码发送成功',
            complete: (res) => {
              if (res.cancel) {
                
              }
          
              if (res.confirm) {
                
              }
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: response.data.msg,
            complete: (res) => {
              if (res.cancel) {
                
              }
          
              if (res.confirm) {
                
              }
            }
          })
        }
      }
    })
  },
    
  timer: function () {
    let promise = new Promise((resolve, reject) => {
      let setTimer = setInterval(
        () => {
          var second = this.data.second - 1;
          this.setData({
            second: second,
            btnValue: second+'秒',
            btnDisabled: true
          })
          if (this.data.second <= 0) {
            this.setData({
              second: 60,
              btnValue: '获取验证码',
              btnDisabled: false
            })
            resolve(setTimer)
          }
        }
        , 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },

  goto_zhuce: function (res) {
    wx.navigateTo({
      url: '../zhuce/zhuce?type=' + res.currentTarget.dataset.type,
    })
  },

  goto_update: function (res) {
    wx.navigateTo({
      url: '../forgetPassword/forgetPassword',
    })
  },

})
